package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.LoginDto;
import com.zzyl.entity.SysUser;
import com.zzyl.mapper.LoginMapper;
import com.zzyl.service.LoginService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public SysUserVo login(LoginDto loginDto) {
        SysUser sysUser = loginMapper.login(loginDto);

        if (sysUser != null) {
            Map<String, Object> claims = new HashMap<>();
            String username = sysUser.getUsername();
            claims.put("username", username);

            String jwt = JwtUtil.createJWT("itheima", 604800000, claims);
            SysUserVo sysUserVo = BeanUtil.toBean(sysUser, SysUserVo.class);
            sysUserVo.setUserToken(jwt);

            return sysUserVo;
        }

        return null;
    }
}
